import { LoginWxMinResult } from "@/apis/login";
import { defineStore } from "pinia";
import { computed, ref } from "vue";

// 定义store
export const useMemberStore = defineStore("member", () => {
  // 定义会员
  const profile = ref<LoginWxMinResult>({} as LoginWxMinResult)
  // 存值
  const setProfile = (val: LoginWxMinResult) => profile.value = val
  // 清空值
  const clearProfile = () => profile.value = {} as LoginWxMinResult
  // 登录状态
  const isLogin = computed(() => Boolean(profile.value.token))
  // 更新图片
  const updateAvatar = (avatar: string) => profile.value.avatar = avatar
  return {
    profile,
    setProfile,
    isLogin,
    clearProfile,
    updateAvatar
  }
},
  {
    // persist: true, 这种形式仅在网页版可行，在小程序不可行，因为俩者存储的形式不同
    persist: {
      storage: {
        setItem(key: string, value: string) {
          uni.setStorageSync(key, value)
        },
        getItem(key: string) {
          return uni.getStorageSync(key)
        },
      }
    }
  })